package com.gxljc.bear.crawler.base;

import java.util.concurrent.ConcurrentHashMap;

import com.gxljc.commons.mongo.base.MongoTemplateFactory;
import com.gxljc.commons.mongo.cli.BaseMongoRepository;

public class BaseMongoTools {

	private final static ConcurrentHashMap<String, BaseMongoRepository> repoMap = new ConcurrentHashMap<>();

	public static BaseMongoRepository getInstance(String dbName) {
		BaseMongoRepository repo = repoMap.get(dbName);
		if (repo == null) {
			synchronized (BaseMongoRepository.class) {
				if (repo == null) {
					String baseDbUri = DolphinCrawlerConf.getInstance()
							.get(DolphinCrawlerConsts.PARAM_MONGO_BASE_URL);
					String dbUri = String.format("%s%s%s", baseDbUri,
							(baseDbUri.endsWith("/") ? "" : "/"), dbName);
					repo = new BaseMongoRepository(MongoTemplateFactory.getMongoTemplate(dbUri));
					repoMap.put(dbName, repo);
				}
			}
		}
		return repo;
	}
}
